package com.jic.veteran.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.jic.veteran.component.VeteranSecurityContext;
import com.jic.veteran.mapper.ProjectBasicMapper;
import com.jic.veteran.mapper.ProjectMessageMapper;
import com.jic.veteran.model.ProjectBasic;
import com.jic.veteran.model.ProjectMessage;
import com.jic.veteran.model.ProjectMessageExample;
import com.jic.veteran.service.ProjectMessageService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ProjectMessageServiceImpl implements ProjectMessageService {
    private static final Logger LOGGER = LoggerFactory.getLogger(ProjectMessageServiceImpl.class);
    @Autowired
    private ProjectMessageMapper projectMessageMapper;
    @Autowired
    private ProjectBasicMapper projectBasicMapper;

    @Override
    public List<ProjectMessage> list(
        String projectId, Boolean show, Integer pageSize, Integer pageNum) {
        PageHelper.startPage(pageNum,pageSize);
        ProjectMessageExample example = new ProjectMessageExample();
        ProjectMessageExample.Criteria criteria = example.createCriteria();
        if(StrUtil.isNotEmpty(projectId)) {
          criteria.andProjectIdLike("%" + projectId + "%" );
        }
      if (show != null) {
        criteria.andIsShow(show);
      }
        return projectMessageMapper.selectByExample(example);
    }

    @Override
    public int create(Long id,ProjectMessage projectMessage) {
        ProjectBasic projectBasic = projectBasicMapper.selectByPrimaryKey(id);
        if(null != projectBasic) {
            projectMessage.setProjectName(projectBasic.getProjectName());
            projectMessage.setProjectId(projectBasic.getProjectId());
            projectMessage.setCreateBy(VeteranSecurityContext.getUserName());
            projectMessage.setCreateTime(DateUtil.date());
            projectMessage.setModifyBy(VeteranSecurityContext.getUserName());
            projectMessage.setModifyTime(DateUtil.date());
            return projectMessageMapper.insert(projectMessage);
        }
        return 0;
    }

    @Override
    public int update(Long id,ProjectMessage projectMessage) {
        ProjectMessage projectMessageUpdate = projectMessageMapper.selectByPrimaryKey(id);
        if(null != projectMessageUpdate) {
            projectMessageUpdate.setId(id);
            projectMessageUpdate.setModifyBy(VeteranSecurityContext.getUserName());
            projectMessageUpdate.setModifyTime(DateUtil.date());
            projectMessageUpdate.setProjectReply(projectMessage.getProjectReply());
            projectMessageUpdate.setShow(projectMessage.isShow());
            return projectMessageMapper.updateByPrimaryKeySelective(projectMessageUpdate);
        }
        return 0;
    }

    @Override
    public ProjectMessage getItem(Long id) {
        return projectMessageMapper.selectByPrimaryKey(id);
    }

    @Override
    public int delete(Long id) {
        return projectMessageMapper.deleteByPrimaryKey(id);
    }
}
